<template>
  <NavBar title="我的购物车"></NavBar>
  <div class="list" v-if="state.list.length">
    <div class="list__item" v-for="(item,index) in state.list" :key="index">
      <div class="list__item-left">
        <van-checkbox @click="changeItem" checked-color="#A8E063" v-model="item.check"></van-checkbox>
      </div>
      <div class="list__item-right">
        <div class="right__left">
          <img @click="img(item.product.image_urls)" :src="item.product.image_urls" alt="">
        </div>
        <div class="right__right">
          <div class="right__right-title">{{ item.product.product_name }}</div>
          <div class="right__right-des">
            <span>{{item.product_material}}{{item.product_purity}}</span>
            <span>{{ item.material_weight }} g</span>
          </div>
          <div class="right__right-stepper">
            <van-stepper @change="changeNumber($event,item)" v-model="item.num" theme="round" button-size="22" disable-input/>
            <div class="stepper__deleted" @click="delet(item.id)">删除</div>
          </div>

        </div>
      </div>
    </div>
  </div>
  <div class="footer">
    <div>
      <van-checkbox checked-color="#A8E063" @click="changeAll" v-model="state.allCheck">全选</van-checkbox>
    </div>
    <div class="footer__right" @click="confirm">提交订单</div>
  </div>
</template>

<script setup>
import NavBar from "@/components/NavBar/index.vue"
import {onMounted, ref, watch} from "vue";
import {addOrder, cartEdit, getCart} from "../../api/index.js";
import {Dialog, ImagePreview, Toast} from "vant"
import {useRouter} from "vue-router";
const router=useRouter()
const state = ref({
  allCheck: false,
  list: []
})
const init = () => {
  getCart().then(res => {
    state.value.list = res.data;
  })
}
const changeAll = (e) => {
  // console.log(e)
  state.value.list.forEach(ele => {
    ele.check = state.value.allCheck;
  })
}
const changeItem = () => {
  if (state.value.list.every(ele => ele.check)) {
    state.value.allCheck = true
  } else {
    state.value.allCheck = false

  }
}
const img=(val)=>{
  ImagePreview([val])
}
const changeNumber=(event,value)=>{
  console.log(event,value,"111")
  cartEdit({
    type:"change",
    cart_id:value.id,
    product_id:value.product_id,
    num:value.num
  })
}
const confirm=async ()=>{
  if(state.value.list.filter(ele=>ele.check).length){
    const res=await addOrder({
      cart_ids:state.value.list.filter(ele=>ele.check).map(ele=>ele.id).join(',')
    })
    if(res.code==1){
      router.replace("/My")
    }
  }else{
    Toast("请选择")
  }

}
const delet = (id) => {
  Dialog.confirm({
    title: "确认删除", showCancelButton: true,
    beforeClose(val, done, canceled) {
      // console.log(val)
      if (val == "confirm") {
        cartEdit({
          type: "del",
          cart_id: id
        }).then((res) => {
          Toast(res.msg)
          init()
        })
        return true
      } else {
        return true
      }
      // canceled()
    }
  })
}
onMounted(() => {
  init()
})
</script>

<style lang="less" scoped>
.list {
  &__item {
    display: flex;
    padding-left: 8px;
    padding-right: 20px;
    align-items: center;
    padding-top: 17px;

    &-right {
      display: flex;
      flex: 1;

      .right {
        &__left {
          margin-left: 34px;
          margin-right: 25px;

          img {
            width: 70px;
            height: 70px;
          }
        }

        &__right {
          width: 100%;

          &-title {
            font-size: 15px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
          }

          &-des {
            font-size: 13px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
            display: flex;
            justify-content: space-between;
          }

          &-stepper {
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
        }
      }
    }
  }
}

.footer {
  width: 375px;
  height: 50px;
  background: #FFFFFF;
  position: fixed;
  bottom: 0;
  padding-left: 15px;
  display: flex;
  align-items: center;
  left: 0;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;
  right: 0;
  justify-content: space-between;

  &__right {
    text-align: center;
    line-height: 50px;
    width: 130px;
    font-size: 16px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #FFFFFF;
    height: 50px;
    background: linear-gradient(315deg, #56AB2F 0%, #A8E063 100%);
  }
}

.stepper__deleted {
  font-size: 13px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #A9A9A9;
}
</style>
